﻿namespace Anley.DomainDrivenDesign.Contract.Validate;

/// <summary>
/// API显示元数据提供程序
/// </summary>
internal class ApiDisplayMetadataProvider
    : IDisplayMetadataProvider
{
    private ResourceManager _resourceManager;

    public ApiDisplayMetadataProvider(
        ResourceManager resourceManager)
    {
        _resourceManager = resourceManager;
    }

    /// <summary>
    /// 创建显示元数据
    /// </summary>
    public void CreateDisplayMetadata(
        DisplayMetadataProviderContext context)
    {
        var attributes = context.Attributes;
        var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
        if (displayAttribute != null)
            return;
        if (!string.IsNullOrWhiteSpace(context.Key.Name))
            context.DisplayMetadata.DisplayName =
                () => _resourceManager.GetString(context.Key.Name, CultureInfo.CurrentCulture);
    }
}